package org.example.StudentManagementSystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.StudentManagementSystem.entity.Teacher;
import org.example.StudentManagementSystem.common.Result;
import org.example.StudentManagementSystem.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/teacher")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
@Tag(name = "教师管理", description = "教师管理相关的接口")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 获取教师列表
     */
    @Operation(summary = "获取教师列表", description = "分页获取教师列表")
    @Parameters({
            @Parameter(name = "page", description = "页码", required = true, schema = @Schema(type = "integer", defaultValue = "1")),
            @Parameter(name = "size", description = "每页大小", required = true, schema = @Schema(type = "integer", defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "获取教师列表成功", content = @Content(schema = @Schema(implementation = Result.class)))
    @GetMapping("")
    public Result getTeacherList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        Page<Teacher> teacherPage = teacherService.page(Page.of(page, size));
        if (teacherPage.getTotal() == 0) {
            return Result.error("暂无数据");
        }
        return Result.success(teacherPage, "获取教师列表成功");
    }

    /**
     * 获取所有教师
     */
    @Operation(summary = "获取所有教师", description = "获取所有教师信息")
    @ApiResponse(responseCode = "200", description = "获取教师列表成功", content = @Content(schema = @Schema(implementation = Result.class)))
    @GetMapping("/all")
    public Result getAllTeacher() {
        return Result.success(teacherService.list(), "获取教师列表成功");
    }

    /**
     * 获取单个教师信息
     */
    @Operation(summary = "获取单个教师信息", description = "根据ID获取教师信息")
    @Parameter(name = "id", description = "教师ID", required = true, schema = @Schema(type = "integer"))
    @ApiResponse(responseCode = "200", description = "获取教师信息成功", content = @Content(schema = @Schema(implementation = Result.class)))
    @ApiResponse(responseCode = "404", description = "没有该教师", content = @Content(schema = @Schema(implementation = Result.class)))
    @GetMapping("/{id}")
    public Result getTeacherById(@PathVariable int id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            return Result.error("没有该教师");
        }
        return Result.success(teacher, "获取教师信息成功");
    }

    /**
     * 添加教师
     */
    @Operation(summary = "添加教师", description = "添加新的教师信息")
    @ApiResponse(responseCode = "200", description = "教师添加成功", content = @Content(schema = @Schema(implementation = Result.class)))
    @ApiResponse(responseCode = "400", description = "教师名不能为空", content = @Content(schema = @Schema(implementation = Result.class)))
    @PostMapping("")
    public Result addTeacher(@RequestBody @Schema(description = "教师信息", required = true) Teacher teacher) {
        if (teacher.getName() == null) {
            return Result.error("教师名不能为空");
        }
        return teacherService.save(teacher) ? Result.success(teacher, "教师添加成功") : Result.error("教师添加失败");
    }

    /**
     * 修改教师
     */
    @Operation(summary = "修改教师", description = "更新指定教师的信息")
    @Parameters({
            @Parameter(name = "id", description = "教师ID", required = true, schema = @Schema(type = "integer")),
            @Parameter(name = "teacher", description = "教师信息", required = true, schema = @Schema(implementation = Teacher.class))
    })
    @ApiResponse(responseCode = "200", description = "教师修改成功", content = @Content(schema = @Schema(implementation = Result.class)))
    @ApiResponse(responseCode = "400", description = "教师名不能为空", content = @Content(schema = @Schema(implementation = Result.class)))
    @ApiResponse(responseCode = "404", description = "没有该教师", content = @Content(schema = @Schema(implementation = Result.class)))
    @PutMapping("/{id}")
    public Result updateTeacher(@PathVariable int id, @RequestBody Teacher teacher) {
        if (teacher.getName() == null) {
            return Result.error("教师名不能为空");
        }
        teacher.setId(id);
        return teacherService.updateById(teacher) ? Result.success(teacher, "教师修改成功") : Result.error("教师修改失败");
    }

    /**
     * 删除教师
     */
    @Operation(summary = "删除教师", description = "根据ID删除教师")
    @Parameter(name = "id", description = "教师ID", required = true, schema = @Schema(type = "integer"))
    @ApiResponse(responseCode = "200", description = "教师删除成功", content = @Content(schema = @Schema(implementation = Result.class)))
    @ApiResponse(responseCode = "404", description = "没有该教师", content = @Content(schema = @Schema(implementation = Result.class)))
    @DeleteMapping("/{id}")
    public Result deleteTeacher(@PathVariable int id) {
        if (teacherService.getById(id) == null) {
            return Result.error("没有该教师");
        }
        return teacherService.removeById(id) ? Result.success("教师删除成功") : Result.error("教师删除失败");
    }

    /**
     * 批量删除教师
     */
    @Operation(summary = "批量删除教师", description = "根据ID列表批量删除教师")
    @Parameter(name = "ids", description = "教师ID列表", required = true, schema = @Schema(type = "array", implementation = Integer.class))
    @ApiResponse(responseCode = "200", description = "批量删除教师成功", content = @Content(schema = @Schema(implementation = Result.class)))
    @ApiResponse(responseCode = "400", description = "请选择要删除的教师或没有该教师", content = @Content(schema = @Schema(implementation = Result.class)))
    @DeleteMapping("")
    public Result deleteTeachers(@RequestBody int[] ids) {
        if (ids.length == 0) {
            return Result.error("请选择要删除的教师");
        }
        for (int id : ids) {
            if (teacherService.getById(id) == null) {
                return Result.error("没有该教师");
            }
        }
        List<Integer> idList = Arrays.stream(ids).boxed().collect(Collectors.toList());
        return teacherService.removeByIds(idList) ? Result.success("批量删除教师成功") : Result.error("批量删除教师失败");
    }
}
